<?php
class MysqlDAL{	
	
	public $con;	
	
	//init method
	function MysqlDAL($db)
	{	
		$this->con = Propel::getConnection($db);
	}
		
/**
 * function to get data from database using creole
 * 
 * @param $sql: sql statement
 * @param $db
 * 
 * return: 
 */
	public function doSelect($sql, $mode = ResultSet::FETCHMODE_ASSOC)
	{    
		try {
			$stmt = $this->con->prepareStatement($sql);
			$rs = $stmt->executeQuery($mode,true);
		} catch (Exception $e) {			
			if ($stmt) $stmt->close();			
			Propel::log($e->getMessage(), Propel::LOG_ERR);						
			throw new PropelException($e);			
		}
		
		$result = array();
	  	foreach ($rs as $row)
		{
            $result[] = $row;
        }		
		return $result;
	}	

	/*
	 * method to update, insert, delete data
	 * 
	 */
	public function doUpdate($sql)
	{	
	  	$stmt = $this->con->prepareStatement($sql);	  	
		try
	  	{		  	
		  	$stmt->executeUpdate();
	  	}
	  	catch ( Exception  $e)
	  	{	
	  		if ($stmt) $stmt->close();
	  		Propel::log($e->getMessage(), Propel::LOG_ERR);
			throw new PropelException($e);	
	  	}
	}
	
	public function safe($data, $quote = 1)
	{		
		if ($data != '') 
		{			
			$data = mysql_real_escape_string($data, $this->con->getResource());
			
			if ($quote)
			{
				return "'".$data."'";
			}else
			{
				return $data;
			}
		}	
			
		return $data;
	}
}
?>